//
//  LKMyQustionCollectionViewController.m
//  YLHelper
//
//  Created by she1323 on 2018/5/10.
//  Copyright © 2018年 she1323. All rights reserved.
//

#import "LKMyQustionCollectionViewController.h"
#import "LKTestpageNewBottomCell.h"
#import "LKTestpageNameLOldCell.h"
#import "LKMyCollectionQustionModel.h"
#import "LKTestChooseCell.h"
#import "LKTestAnswerAnalysicCell.h"
#import "LKTestAnswerCell.h"
@interface LKMyQustionCollectionViewController ()<UITableViewDelegate,UITableViewDataSource>
@property(nonatomic,strong)LKBaseTableview *tableView;
@property(nonatomic,strong)NSIndexPath *selectedIndex;
@property(nonatomic,assign)NSInteger PageIndex;
@property(nonatomic,assign)NSInteger MaxResultCount;
@property(nonatomic,strong)NSMutableArray *dataArray;
@end

@implementation LKMyQustionCollectionViewController

-(LKBaseTableview *)tableView{
    if (!_tableView) {
        _tableView = [LKBaseTableview setTableviewWithFrame:CGRectMake(0, 0, WIDTH, HEIGHT - (NAVIGATION_BAR_HEIGHT + 45) ) style:UITableViewStyleGrouped registercellClasss:@[@"LKTestpageNewBottomCell",@"LKTestpageNameLOldCell",@"LKTestAnswerCell",@"LKTestChooseCell",@"LKTestAnswerAnalysicCell"]];
        [self.view addSubview:_tableView];
        _tableView.delegate = self;
        _tableView.dataSource = self;
        _tableView.emptyDataSetSource = self;
        _tableView.emptyDataSetDelegate = self;
        _tableView.allowsMultipleSelection = YES;
    }
    return _tableView;
}
/**
 头部下拉刷新
 */
-(void)headerReloadData{
    self.PageIndex = 1;
    NSString *pageIndexStr = [NSString stringWithFormat:@"%ld",self.PageIndex];
    NSString *maxRsult = [NSString stringWithFormat:@"%ld",self.MaxResultCount];
    [self getCollectionQustionDataListWithParam:@{@"PageIndex":pageIndexStr,@"MaxResultCount":maxRsult}];
}

/**
 尾部上拉加载更多
 */
-(void)footerReloadData{
    self.PageIndex++;
    NSString *pageIndexStr = [NSString stringWithFormat:@"%ld",self.PageIndex];
    NSString *maxRsult = [NSString stringWithFormat:@"%ld",self.MaxResultCount];
    [self getCollectionQustionDataListWithParam:@{@"PageIndex":pageIndexStr,@"MaxResultCount":maxRsult}];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.tableView.estimatedRowHeight = 200;
    self.PageIndex = 1;
    self.MaxResultCount = 10;
    NSString *pageIndexStr = [NSString stringWithFormat:@"%ld",self.PageIndex];
    NSString *maxRsult = [NSString stringWithFormat:@"%ld",self.MaxResultCount];
    self.tableView.mj_header = [MJRefreshNormalHeader headerWithRefreshingTarget:self refreshingAction:@selector(headerReloadData)];
    self.tableView.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingTarget:self refreshingAction:@selector(footerReloadData)];
    [self getCollectionQustionDataListWithParam:@{@"PageIndex":pageIndexStr,@"MaxResultCount":maxRsult}];
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    LKMyCollectionQustionModel *model = self.dataArray[section];
    return model.questionAnswer.count + 2;
}
-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
  LKMyCollectionQustionModel *model = self.dataArray[indexPath.section];
  LKTestChooseCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LKTestChooseCell"];
    if (indexPath.row == model.questionAnswer.count) {
        LKTestAnswerCell *ansercell = [tableView dequeueReusableCellWithIdentifier:@"LKTestAnswerCell"];
        NSString *rightAnser = @"正确答案：";
        for (LKMyCollectionQustionAnswerModel *ansmodel in model.questionAnswer) {
            if ([ansmodel.isRight integerValue]) {
                rightAnser = [rightAnser stringByAppendingString:ansmodel.answerName];
            }
        }
        [ansercell.righttn setTitle:rightAnser forState:UIControlStateNormal];
        ansercell.yourBtn.hidden = YES;
        return ansercell;
    }
    if (indexPath.row == model.questionAnswer.count + 1) {
        
        LKTestAnswerAnalysicCell *anycell = [tableView dequeueReusableCellWithIdentifier:@"LKTestAnswerAnalysicCell"];
        NSMutableAttributedString *str = [[NSMutableAttributedString alloc] initWithString:[NSString stringWithFormat:@"答案解析：%@",model.analysis]];  
        [str addAttribute:NSForegroundColorAttributeName value:blueDefault range:NSMakeRange(0, 4)];
        [str addAttribute:NSFontAttributeName value:[UIFont systemFontOfSize:15] range:NSMakeRange(0, 4)];
        anycell.contentLabel.attributedText = str;
        return anycell;
    }
    LKMyCollectionQustionAnswerModel *ssModel = model.questionAnswer[indexPath.row];
    cell.mycollectqustStr = [NSString stringWithFormat:@"%@:%@",ssModel.answerName,ssModel.answerContent];
    return cell;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return UITableViewAutomaticDimension;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 13;
}
-(UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIView *headerView = [[UIView alloc] init];
    headerView.backgroundColor = [UIColor whiteColor];
    LKMyCollectionQustionModel *model = self.dataArray[section];
    NSString *questionTitle = [NSString stringWithFormat:@"【%@】%@",model.questionTypeText,model.questionName];
    LKBaseLabel *label = [LKBaseLabel labelWithFrame:CGRectZero text:questionTitle textColor:COLOR_HEX(0x929292) font:[UIFont systemFontOfSize:15] textAlignment:NSTextAlignmentLeft backgroundColor:[UIColor whiteColor]];
    label.numberOfLines = 0;
    [headerView addSubview:label];
    [label mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(headerView.mas_left).offset(16);
        make.right.equalTo(headerView.mas_right).offset(-16);
        make.top.equalTo(headerView.mas_top).offset(21);
        make.bottom.equalTo(headerView.mas_bottom).offset(-21);
    }];
    return headerView;
}
-(UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view = [[UIView alloc] init];
    view.backgroundColor = cellHeaderBgColor;
    return view;
}
-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
}

/**
 获取收藏的题目列表
 */
-(void)getCollectionQustionDataListWithParam:(NSDictionary *)param{
    [[LKNetDataManager defaultManager] sendRequestMethod:HTTPMethodPOST serverUrl:ServerlUrl apiPath:mycollectionPracticeListUrl header:[LKUserInforTool header] parameters:param progress:^(NSProgress * _Nullable progress) {
        
    } success:^(BOOL isSuccess, id  _Nullable responseObject) {
        [self.tableView.mj_header endRefreshing];
        [self.tableView.mj_footer endRefreshing];
        if (self.PageIndex == 1) {
            [self.dataArray removeAllObjects];
        }
        NSArray *items = responseObject[@"result"][@"item"];
        for (NSDictionary *dic in items) {
            LKMyCollectionQustionModel *model = [LKMyCollectionQustionModel modelWithDictionary:dic];
            [self.dataArray addObject:model];
        }
//        if (items.count < self.dataArray.count) {
//            [self.tableView.mj_footer endRefreshingWithNoMoreData];
//        }
        if (self.dataArray.count == 0) {
            self.tableView.mj_footer.hidden = YES;
        }
        [self.tableView reloadData];
    } failure:^(NSString * _Nullable errorMessage) {
        
    }];
}
-(NSMutableArray *)dataArray{
    if (!_dataArray) {
        _dataArray = [[NSMutableArray alloc] init];
    }
    return _dataArray;
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
